package com.ciei.dpagm.service.conditionRule;

import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.enums.ComparatorEnum;
import com.ciei.dpagm.common.enums.ErrorCodeEnum;
import com.ciei.dpagm.service.conditionRule.factMode.FactModeFactoryService;
import com.ciei.dpagm.service.conditionRule.standardSpecification.StandardSpecificationFactoryService;
import com.ciei.dpagm.util.JsonUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.regex.Pattern;

/**
 * @author pp
 * @date 2023/03/19
 */
@Service
public class CompareService {

    public static String title = "比较符";

    private static String dataNotMeetRequirements = "数据不符合要求：";

    /**
     * 比较
     * @param factModeResultValue 事实方式结果值
     * @param comparator 比较符
     * @param standardSpecificationResultValue 标准规范结果值
     * @return
     */
    public JSONObject compare(Object factModeResultValue,Integer comparator,Object standardSpecificationResultValue){
        ComparatorEnum comparatorEnum = ComparatorEnum.getEnumByValue(comparator);
        if(comparatorEnum == null){
            return ConditionHandleService.notMatchAnalyzeMessage(title);
        }
        switch (comparatorEnum) {
            case CONTAIN:
                if(factModeResultValue instanceof Collection){
                    Collection<Object> factModeValue = (Collection<Object>)factModeResultValue;
                    if(factModeValue.isEmpty()){
                        return valueIsEmptyMessage(FactModeFactoryService.title);
                    }
                    Object sonFactModeValue = factModeValue.iterator().next();
                    if(sonFactModeValue instanceof String){
                        if(standardSpecificationResultValue instanceof Collection){
                            Collection<String> standardSpecificationValue = (Collection<String>)standardSpecificationResultValue;
                            Boolean isAllContains = true;
                            for(String value : standardSpecificationValue){
                                if(!factModeValue.contains(value)){
                                    isAllContains = false;
                                }
                            }
                            if(isAllContains){
                                return JsonUtil.getSuccess("");
                            }
                            return dataMatchFailMessage();
                        }else{
                            String standardSpecificationValue = standardSpecificationResultValue.toString();
                            if(factModeValue.contains(standardSpecificationValue)){
                                return JsonUtil.getSuccess("");
                            }
                            return dataMatchFailMessage();
                        }
                    }else if(sonFactModeValue instanceof Collection){
                        Collection<Collection<String>> factModeValueCollection = (Collection<Collection<String>>)factModeResultValue;
                        if(standardSpecificationResultValue instanceof Collection){
                            Collection<String> standardSpecificationValue = (Collection<String>)standardSpecificationResultValue;
                            Boolean isAllContains = true;
                            for(Collection<String> sonFactModeValueCollection : factModeValueCollection) {
                                for(String value : standardSpecificationValue){
                                    if(!sonFactModeValueCollection.contains(value)){
                                        isAllContains = false;
                                    }
                                }
                            }
                            if(isAllContains){
                                return JsonUtil.getSuccess("");
                            }
                            return dataMatchFailMessage();
                        }else{
                            String standardSpecificationValue = standardSpecificationResultValue.toString();
                            Boolean isAllContains = true;
                            for(Collection<String> sonFactModeValueCollection : factModeValueCollection) {
                                if(!sonFactModeValueCollection.contains(standardSpecificationValue)){
                                    isAllContains = false;
                                }
                            }
                            if(isAllContains){
                                return JsonUtil.getSuccess("");
                            }
                            return dataMatchFailMessage();
                        }

                    }

                }
                return notSupportValueTypeMessage(FactModeFactoryService.title);
            case EQUAL:
                if(factModeResultValue instanceof Collection){
                    Collection<String> factModeValueList = (Collection<String>)factModeResultValue;
                    if(factModeValueList.isEmpty()){
                        return valueIsEmptyMessage(FactModeFactoryService.title);
                    }
                    if(standardSpecificationResultValue instanceof String){
                        Boolean isAllMatch = true;
                        for(String factModeValue : factModeValueList){
                            if(!factModeValue.equals(standardSpecificationResultValue)){
                                isAllMatch = false;
                            }
                        }
                        if(isAllMatch){
                            return JsonUtil.getSuccess("");
                        }
                        return dataMatchFailMessage();
                    }
                }else if(factModeResultValue instanceof String){
                    String equalFactModeValue = factModeResultValue.toString();
                    String equalStandardSpecificationValue = standardSpecificationResultValue.toString();
                    if(equalFactModeValue.equals(equalStandardSpecificationValue)){
                        return JsonUtil.getSuccess("");
                    }
                    return dataMatchFailMessage();
                }
                return notSupportValueTypeMessage(FactModeFactoryService.title);
            default:
                return ConditionHandleService.notMatchAnalyzeMessage(title);
        }
    }

    /**
     * 值为空消息
     * @param title
     * @return
     */
    private static JSONObject valueIsEmptyMessage(String title){
        return JsonUtil.getFail(ErrorCodeEnum.DATA_NOT_FOUND,dataNotMeetRequirements + "解析出来的" + title + "值为空");
    }

    /**
     * 数据匹配失败消息
     * @return
     */
    private static JSONObject dataMatchFailMessage(){
        return JsonUtil.getFail(ErrorCodeEnum.DATA_MATCH_FAIL,dataNotMeetRequirements + "解析的事实方式值或标准规范值不符合要求");
    }

    /**
     * 不支持该值类型消息
     * @param title
     * @return
     */
    private static JSONObject notSupportValueTypeMessage(String title){
        return JsonUtil.getFail(ErrorCodeEnum.DATA_OPERATION_DENY,dataNotMeetRequirements + "不支持该" + title +"值数据类型解析");
    }

    /**
     * 值类型不匹配消息
     * @param title
     * @return
     */
    private static JSONObject valueTypeNotMatchMessage(String title,String type){
        return JsonUtil.getFail(ErrorCodeEnum.DATA_OPERATION_DENY,dataNotMeetRequirements + title + "值类型不为<" + type + ">");
    }
}
